<template>
  <div class="container">
    <div class="btnClose">
      <router-link to="/" class="iconfont iconicon-test"></router-link>
    </div>
    <div class="logo">
      <span class="iconfont iconnew"></span>
    </div>
    <!-- 输入框组件 -->
    <MyInput
      :rule="/^\w{3,8}$/"
      type="text"
      placeholder="用户名 | 手机号码"
      ellMsg="请输入正确的用户名"
      @setValue="setUsername"
    />
    <MyInput
      :rule="/^\w{3,8}$/"
      type="text"
      placeholder="昵称"
      ellMsg="请输入合法的昵称"
      @setValue="setNickname"
    />
    <MyInput
      :rule="/^\d{3,12}$/"
      type="password"
      placeholder="密码"
      ellMsg="密码仅限6至12的数字"
      @setValue="setPassword"
    />
    <!-- 按钮组件 -->
    <MyButton value="注册" @click.native="register" />
  </div>
</template>

<script>
//组件引入
import MyInput from "@/components/MyInput";
import MyButton from "@/components/MyButton";
export default {
  components: {
    MyInput,
    MyButton,
  },
  data() {
    return {
      username: "",
      nickname: "",
      password: "",
    };
  },
  methods: {
    setUsername(newValue) {
      this.username = newValue;
    },
    setNickname(newValue) {
      this.nickname = newValue;
    },
    setPassword(newValue) {
      this.password = newValue;
    },
    register() {
      if (this.username && this.nickname && this.password) {
        this.$axios({
          method: "post",
          url: "/register",
          data: {
            username: this.username,
            nickname: this.nickname,
            password: this.password,
          },
        }).then((res) => {
          console.log(res);
          if (res.data.message === "注册成功") {
            this.$toast(res.data.message);
            this.$router.push("/login");
            // 因为无论如何都返回 200 响应码 总是会走成功的逻辑, 这种风格的错误, 只会在返回的数据层面上报错,
            // 只能在 .then 成功逻辑上面进行处理
          }
        });
      } else {
        this.$toast("输入不能为空");
      }
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  padding: 24/360 * 100vw;
}
.btnClose {
  .iconfont {
    font-size: 28/360 * 100vw;
  }
}
.logo {
  text-align: center;
  .iconfont {
    font-size: 126/360 * 100vw;
    color: #d81e06;
  }
}
</style> 